package com.weimob.mcloud.wechatwork.customer.third;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.google.common.collect.Lists;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.bc.response.BcCorpInfoDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.bos.request.QueryProductInstancePageByBosDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.bos.response.ProductInstanceDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.bos.response.StructureTreeNodeResponseDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.request.ChannelAppRequestDTO;
import com.weimob.mcloud.wechatwork.core.adapter.service.BosAdapterService;
import com.weimob.mcloud.wechatwork.core.adapter.service.ChannelAdapterService;
import com.weimob.soa.common.response.SoaResponse;
import com.weimob.soa.common.utils.SoaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2021/11/4 11:15 下午
 */
@Component
@Slf4j
public class BosThird {

    @Autowired
    private BosAdapterService bosAdapterService;

    @Autowired
    private ChannelAdapterService channelAdapterService;

    /**
     *
     * @param bosId
     * @param productIds
     * @return
     */
    public List<ProductInstanceDTO> queryProductInstanceDTO(Long bosId, List<Long> productIds) {

        QueryProductInstancePageByBosDTO queryProductInstancePageByBosDTO = new QueryProductInstancePageByBosDTO();
        queryProductInstancePageByBosDTO.setBosId(bosId);
        queryProductInstancePageByBosDTO.setProductIds(productIds);

        try {
            SoaResponse<Page<ProductInstanceDTO>, Void> pageVoidSoaResponse = bosAdapterService.queryProductInstancePageByBos(queryProductInstancePageByBosDTO);
            Page<ProductInstanceDTO> productInstanceDTOPage = SoaUtil.unpack(pageVoidSoaResponse);
            if (Objects.isNull(productInstanceDTOPage)) {
                return new ArrayList<>();
            }
            return productInstanceDTOPage.getResult();
        } catch (Exception e) {
            log.error("查询产品实例id失败, request:{}, error:{}", JSON.toJSONString(queryProductInstancePageByBosDTO), e);
            return new ArrayList<>();
        }
    }

    /**
     * 查询cdp产品实例
     * @param bosId
     * @return
     */
    public ProductInstanceDTO getCdpProductInstance(Long bosId) {
        //获取cdp产品实例id
        List<ProductInstanceDTO> productInstanceDTOS = queryProductInstanceDTO(bosId, Lists.newArrayList(204L));
        if (CollectionUtils.isEmpty(productInstanceDTOS)) {
            return null;
        }

        //获取cdp产品实例id
        return productInstanceDTOS.get(0);
    }

    /**
     *
     * @param bosId
     * @return
     */
    public Long queryRootVidByBosId(Long bosId) {
        if (null == bosId) {
            return null;
        }

        SoaResponse<StructureTreeNodeResponseDTO, Void> voidSoaResponse = bosAdapterService.queryRootVidByBosId(bosId);
        if (0L != Long.parseLong(voidSoaResponse.getReturnCode())) {
            log.error("根据bosId查询根节点失败, bosId:{}, msg:{}", bosId, voidSoaResponse.getReturnMsg());
            return null;
        }

        StructureTreeNodeResponseDTO structureTreeNodeResponseDTO = voidSoaResponse.getResponseVo();
        if (Objects.isNull(structureTreeNodeResponseDTO)) {
            return null;
        }

        return structureTreeNodeResponseDTO.getVid();
    }

    public Set<Long> getBosIdListByCorpId(String corpId) {
        ChannelAppRequestDTO channelAppRequestDTO = new ChannelAppRequestDTO();
        channelAppRequestDTO.setCorpId(corpId);
        channelAppRequestDTO.setFromNewCenter(true);
        SoaResponse<List<BcCorpInfoDTO>, Void> bcCorpInfoDTOS = channelAdapterService.getCorpInfoListByCorpId(channelAppRequestDTO);
        if (0L != Long.parseLong(bcCorpInfoDTOS.getReturnCode())) {
            log.error("根据corpId查询bosId失败, corpId:{}, msg:{}", corpId, bcCorpInfoDTOS.getReturnMsg());
            return new HashSet<>();
        }

        List<BcCorpInfoDTO> bcCorpInfoDTOList = bcCorpInfoDTOS.getResponseVo();
        if (CollectionUtils.isEmpty(bcCorpInfoDTOList)) {
            return new HashSet<>();
        }

        Set<Long> bosIds = new HashSet<>();
        for (BcCorpInfoDTO bcCorpInfoDTO : bcCorpInfoDTOList) {
            bosIds.add(bcCorpInfoDTO.getBosId());
        }
        return bosIds;
    }
}
